<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :visible.sync="openCraft"
      width="75%"
      append-to-body
    >
      <el-form
        ref="carftForm"
        :model="carftForm"
        :rules="carftForm.doType == '1' ? rules : rulesa"
        label-width="110px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编号" prop="workorderCode">
              <el-input
                v-model="carftForm.workorderCode"
                placeholder="请输入工单编号"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据状态" prop="status">
              <el-select
                v-model="carftForm.status"
                disabled
                placeholder="请选择单据状态"
              >
                <el-option
                  v-for="dict in dict.type.mes_order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productCode">
              <el-input
                v-model="carftForm.productCode"
                placeholder="请选择产品"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input
                v-model="carftForm.productName"
                placeholder="请选择产品"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产车间" prop="workshopId">
              <el-select
                v-model="carftForm.workshopId"
                placeholder="请选择生产车间"
                @change="selectTrigger(carftForm.workshopId)"
                :disabled="carftForm.doType == '1' ? true : false"
              >
                <el-option
                  v-for="item in workshopList"
                  :key="item.workshopId"
                  :label="item.workshopName"
                  :value="item.workshopId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产产线" prop="lineId">
              <!-- multiple -->
              <el-select
                v-model="carftForm.lineId"
                placeholder="请选择产线"
                :disabled="carftForm.doType == '1' ? true : false"
              >
                <el-option
                  v-for="item in productionlineList"
                  :key="item.lineId"
                  :label="item.lineName"
                  :value="item.lineId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="工单工艺路线" prop="routeName">
              <el-input
                v-model="carftForm.routeName"
                placeholder="请输入工单工艺路线"
                disabled
              />
              <!--              <el-select v-model="carftForm.routeId"  placeholder="请选择" >-->
              <!--                <el-option v-for="item in carftList" :key="item.routeId" :label="item.carftName" :value="item.routeId">-->
              <!--                </el-option>-->
              <!--              </el-select>-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划开工时间" prop="fPlanStartDate">
              <el-date-picker
                v-model="carftForm.fPlanStartDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期时间"
                disabled
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计划完工时间" prop=" fPlanFinishDate">
              <el-date-picker
                v-model="carftForm.fPlanFinishDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期时间"
                disabled
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="实际开工时间" prop="actualStartTime">
              <el-date-picker
                v-model="carftForm.actualStartTime"
                type="date"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd"
                :disabled="carftForm.doType == '1' ? true : false"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="实际完工时间" prop="actualFinisihTime">
              <el-date-picker
                v-model="carftForm.actualFinisihTime"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期时间"
                :disabled="carftForm.doType == '1' ? true : false"
              ></el-date-picker>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <el-tag
        size="large"
        style="font-size: 20px; margin-bottom: 10px; margin-top: 10px"
        >工艺维护数据</el-tag
      >
      <hr />
      <el-tabs type="border-card" v-if="processList.length > 0">
        <el-tab-pane
          v-for="(process, index) in processList"
          :key="index"
          :label="process.processName"
        >
          <Routeproductbom v-if="carftForm.doType =='2' "
             ref="productbom"
            :carftForm="carftForm"
            :routeId="carftForm.routeId"
            :productId="carftForm.productId"
            :processId="process.processId"
            :processName="process.processName"
            :processCode="process.processCode"
            :buttonType="carftForm.doType"
            :optType="optType"
            @backEndData="getSubassembly"
            :subassemblyList="subassemblyList" ></Routeproductbom>
        </el-tab-pane>
      </el-tabs>
      <el-empty
        v-else
        description="该生产工单缺少工艺路径的维护，请先维护工艺路径！！！"
        image-size="50"
      ></el-empty>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType == 'view'"
          >返回</el-button
        >
        <el-button type="primary" @click="submitForms" v-else>保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
        
  <script>
import { getWorkorder, generateReport } from "@/api/mes/pro/workorder";
export default {
  data() {
    return {
        openCraft: false,
      title: "生产汇报单",
      // 表单参数
      form: {},
      requisitionForm: {
        snList: []
      },
      loading: false,
    };
  },
  methods: {
    //初始化函数
    init(row) {
      console.log(row, "子组件的row");
      this.form = row;
      //调接口需要的id
      this.workorderId = row.workorderId;
      this.openCraft = true;
      this.snList = [];
      this.checkAllLIst = [];
      this.getList();
    },
    getList() {
      this.loading = true;
      getWorkorder(this.form.workorderId).then(response => {
        this.requisitionForm = { ...response.data };
        console.log(this.requisitionFormm, "====== this.requisitionForm=====");
        this.workorderSnList = response.workorderSnList || [];
        let list = [];
        for (let i = 0; i < response.workorderSnList.length; i++) {
          if (response.workorderSnList[i].isCheck != 1) {
            list.push(response.workorderSnList[i].snCode);
          }
        }
        this.checkAllLIst = list;
        // this.checkAllLIst = this.workorderSnList.filter(item=>{
        //   return item.isCheck !='1'
        // })

        console.log(this.checkAllLIst, "======this.checkAllLIst======");
        this.title = "生产汇报单";
        // this.$set(this.form, "routeName", routeName);
      });
    },
    // 取消按钮
    cancel() {
      this.openCraft = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.requisitionForm = {
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        orderSource: null,
        sourceCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpc: null,
        unitOfMeasure: null,
        quantity: null,
        quantityProduced: null,
        quantityChanged: null,
        quantityScheduled: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        requestDate: null,
        parentId: null,
        status: 0,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        orderType: null,
        fPlanStartDate: null,
        fPlanFinishDate: null
      };
      this.autoGenFlag = false;
      this.snList = [];
      this.resetForm("requisitionForm");
    },

    // 生成汇报单
    handleProductQuantity() {
      // workorderSnList
      let list = [];
      list = this.workorderSnList.filter(item => {
        console.log(item.snCode, this.snList.indexOf(item.snCode), "=========");
        return this.snList.indexOf(item.snCode) !== -1;
      });
      console.log(list, "=====list====");
      this.requisitionForm.reportSnList = this.snList;
      console.info(
        this.snList.length,
        "===requisitionForm====snList>",
        this.requisitionForm
      );
      if (this.snList.length != Number(this.requisitionForm.reportQuantity)) {
        return this.$modal.msgError("所选SN码数量必须等于成品入库数量！！！");
      }
      generateReport(this.requisitionForm).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.openCraft = false;
        this.reset();
        this.$emit("getList");
        // this.getList();
      });
    },
    handleCheckAllChange(val) {
      this.snList = val ? this.checkAllLIst : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.checkAllLIst.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.checkAllLIst.length;
    }
  }
};
</script>
    <style rel="stylesheet/scss" lang="scss" scoped>
.app-container ::v-deep .el-drawer__header {
  padding-top: 10px !important;
  padding-bottom: 5px !important;
  margin-bottom: 10px !important;
}
.dialog-footer {
  padding-top: 10px;
  text-align: right;
}
.titleDiv {
  font-weight: 700 !important;
  font-size: 20px;
}
</style>